const { tool,time,api } = getApp();
Page({

    data: {
        team: {},   // 小组信息
        type: 0,    // 小组类型 0-初始状态 1-我创建的 2-我加入的
        topic: {},  // 课题数据
        info: {},   // 用户信息
        show: false,// 修改信息窗口
        form: {},   // 待修改信息
    },

    // 移除成员
    removeTeam(e) {
        let { open_id } = e.currentTarget.dataset;
        let { id } = this.data.team;
        api.showModal('提示','确定要移除该成员吗？').then(res => {
            if (res) tool.delMember(id,open_id).then(res => {
                api.showToast('移除成功','success');
                this.getData(id);
            })
        })
    },

    // 解散/退出小组
    dissol() {
        let { type,info } = this.data;
        let { id } = this.data.team;
        let msg = type == 1 ? '解散' : '退出';
        api.showModal('提示',`你确定要${msg}小组吗？`).then(res => {
            if (res) {
                if (type == 1) {
                    tool.dissol(id,info.open_id).then(res => {
                        api.showToast('解散成功','success');
                        setTimeout(() => {
                            api.switchTab('/pages/group/list/index');
                        }, 1000);
                    })
                } else {
                    tool.delMember(id,info.open_id).then(res => {
                        api.showToast('退出成功','success');
                        setTimeout(() => {
                            api.switchTab('/pages/group/list/index');
                        }, 1000);
                    })
                }
            }
        })
    },

    // 打开修改信息接口
    editInfo() {
        this.setData({ show: true });
    },

    // 关闭修改窗口
    onClose() {
        this.setData({ show: false });
    },

    // 获取表单数据
    getForm(e) {
        let { detail } = e;
        let { id } = this.data.team;
        detail.id = id;
        detail._code = detail.code;
        delete detail.code;
        tool.editTeamInfo(detail).then(res => {
            this.onClose();
            this.getData(id);
            api.showToast('修改成功','success');
        })
    },

    // 获取数据
    getData(id) {
        tool.getTeamInfo(id).then(team => {
            let { topic_id,name,introduce,code } = team;
            let form = { name,introduce,code };
            this.setData({ form });
            if (topic_id) {
                tool.getTopicDetails(topic_id).then(topic => {
                    this.setData({ topic });
                })
            }
            this.setData({ team });
        })
    },

    // 页面加载
    onLoad(options) {
        let { id,type } = options;
        this.getData(id);
        tool.getUserInfo().then(info => {
            this.setData({ type,info });
        });
    },

    // 分享
    onShareAppMessage() {
        let { team_id,name } = this.data.team;
        let { info } = this.data;
        return {
            title: `${info.nickname}邀请你加入${name}`,
            imageUrl: '/assets/icon/message.png',
            path: `/pages/group/join/index?team_id=${team_id}`
        }
    }
    
})